<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
        :disabled="method==='view'" label-width="120px">
        <el-row :gutter="15">
          <el-col :span="22">
            <el-form-item label="工牌ID" prop="rfid" :rules="[
                  {required: true, message:'工牌ID不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.rfid" placeholder="请填写工牌ID"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="devEui" prop="devEui" :rules="[
                  {required: true, message:'devEUI不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.devEui" placeholder="请填写devEui(唯一)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="devAddr" prop="devAddr" :rules="[
                  {required: true, message:'devAddr不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.devAddr" placeholder="请填写devAddr(唯一)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="AppSKey" prop="appSKey" :rules="[
              {required: true, message:'AppSKey不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.appSKey" placeholder="请填写AppSKey"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="nwkSKey" prop="nwkSKey" :rules="[
              {required: true, message:'nwkSKey不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.nwkSKey" placeholder="请填写nwkSKey"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="工牌类型" prop="cardType" :rules="[
                  {required: true, message:'工牌类型不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.cardType" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('lora_rfid_type')" :key="item.value"
                  :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="22">
            <el-form-item label="clazz" prop="clazz" :rules="[
              {required: true, message:'clazz不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.clazz" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('lora_clazz')" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="fcntBits" prop="fcntBits" :rules="[
              {required: true, message:'fcntBits不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.fcntBits" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('fcntBits')" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="lorawanVersion" prop="lorawanVersion" :rules="[
              {required: true, message:'lorawanVersion不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.lorawanVersion" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('lorawanVersion')" :key="item.value"
                  :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="22">
            <el-form-item label="appEui" prop="appEui" :rules="[
              {required: true, message:'appEui不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.appEui" placeholder="请填写appEui"></el-input>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          rfid: '',
          devEui: '',
          devAddr: '',
          cardType: '',
          appKey: '',
          appSKey: '',
          nwkSKey: '',
          clazz: '',
          fcntBits: '',
          lorawanVersion: '',
          appEui: '',
          sendPeriod: '',
          moveSwitch: '',
          reportNum: '',
          overTime: '',
          rssiThreshold: '',
          searchType: '',
          versionNew: ''
        }
      }
    },
    components: {},
    methods: {
      init(method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建工牌`
        } else if (method === 'edit') {
          this.title = '修改工牌'
        } else if (method === 'view') {
          this.title = '查看工牌'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.rfid = '11223344';
          this.inputForm.devEui = '009569000001D0CF';
          this.inputForm.cardType = '员工工牌';
          this.inputForm.appKey = '719B9E4F656FF1B02BCBDB5DCEBABD6D';
          this.inputForm.clazz = 'CLASS_A';
          this.inputForm.fcntBits = 'FCNT_32';
          this.inputForm.lorawanVersion = 'LoRaWAN_1_0_2';
          this.inputForm.appEui = '1122334444332211';

          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/lora/rfid/jfLoraRfid/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({
              data
            }) => {
              this.inputForm = this.recover(this.inputForm, data.jfLoraRfid)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit() {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/lora/rfid/jfLoraRfid/save`,
              method: 'post',
              data: this.inputForm
            }).then(({
              data
            }) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
